package ru.hh.team02.chat.messaging;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/*
 * deals with sending messages.
 * current version - one blocking queue with lots of messages
 * for each client, handled by one thread
 */
public class MessageSender {
    private static final BlockingQueue<Message> messageQueue = 
            new LinkedBlockingQueue<Message>();
    
    public MessageSender() {
        Thread t = new Thread() {
            public void run () {
                while (true) {
                    sendMessage();
                }
            }
        };
        
        t.start();
    }
    
    public void addMessage(final Message message) {
        messageQueue.add(message);
    }

    public void sendMessage() {
        Message message = null;
        try {
            message = messageQueue.take();
            message.sendMessage();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } 
    }
}
